<?php
require_once(__PHY_LIB.'/class/Item.class.php');

class uUser extends Item {
/// definition and constructor ///
    function uUser($id=0, $field="") {
        Query::Query();
        $this->cfgKeyField("id");
        $this->cfgAllField("id,email,nick,pswd,time_added,time_lastlogin");
        $this->cfgTable("unit_user");
        if ("" == $field || "*" == $field) {
            $this->cfgField($this->mAllField);
        }
        else {
            $this->cfgField($field);
        }
        if (0 < $id) {
            $this->cfgWhere($this->mKeyField . "=" . $id);
            $this->load();
        }
        else {
            $this->set($this->mKeyField, $id);
        }
    }

    function loadByEmail($sEmail) {
        $this->cfgWhere("email='".addslashes($sEmail)."'");
        $this->load();
    }
    function loadByNick($sNick) {
        $this->cfgWhere("nick='".addslashes($sNick)."'");
        $this->load();
    }

    function pswdEncode($sPswd) {
        return md5('pswd'.$sPswd);
    }

    function invalid() {
        if (!strpos($this->get('email'), '@')) {
            return 'email';
        }

        return false;
    }

    function logout() {
        setcookie('u_type', '', time()-86400, '/', '.'.__ENV_DMN); unset($_COOKIE['u_type']);
        setcookie('u_id',   '', time()-86400, '/', '.'.__ENV_DMN); unset($_COOKIE['u_id']);
        setcookie('u_nick', '', time()-86400, '/', '.'.__ENV_DMN); unset($_COOKIE['u_nick']);
        /// 销毁session ///
        unset($_SESSION['u_id']);
        return true;
    }

    function login($sAcct, $sPswd, $bLong=false) {
        if (!$sAcct || !$sPswd) { return false; }
        $oUser = new uUser();
        $oUser->loadByEmail($sAcct);
        if ($oUser->pswdEncode($sPswd) == $oUser->get('pswd')) {
            /// 设置cookie ///
            $_SESSION['u_id'] = $oUser->getID();
            $nExpire = $bLong ? (time() + 86400*30) : null; // 30天cookie过期
            setcookie('u_type', 'user',              $nExpire, '/', '.'.__ENV_DMN); $_COOKIE['u_type'] = 'user';
            setcookie('u_id',   $oUser->getID(),     $nExpire, '/', '.'.__ENV_DMN); $_COOKIE['u_id']   = $oUser->getID();
            setcookie('u_nick', $oUser->get('nick'), $nExpire, '/', '.'.__ENV_DMN); $_COOKIE['u_nick'] = $oUser->get('nick');
            /// 最后登录时间 ///
            $oUser->set('time_lastlogin', date('Y-m-d H:i:s'));
            $oUser->update();
            return true;
        }
        else {
            return false;
        }
    }
} /// end class uUser.


class uUserList extends ItemList {
    /// definition and constructor ///
    function uUserList($tablename=null) {
        ItemList::ItemList();
        $this->cfgClass("uUser");
        $this->cfgKeyField("id");
        $this->cfgAllField("id,email,nick,pswd,time_added,time_lastlogin,sign,identity,gender,photo_s,photo_m,birthday,contact");
        $this->cfgTable(null == $tablename ? "unit_user" : $tablename);
    }

} /// end class uUserList.
?>
